<template>
  <div ref="target">
    <HomePanel title="人气推荐" sub-title="人气爆款 不容错过">
      <div class="box">
        <transition name="fade">
          <ul ref="pannel" class="goods-list" v-if="list.length > 0">
            <li v-for="item in list" :key="item.id">
              <RouterLink to="/">
                <img :src="item.picture" alt="" />
                <p class="name">{{ item.title }}</p>
                <p class="desc">{{ item.alt }}</p>
              </RouterLink>
            </li>
          </ul>
          <!-- 骨架屏> -->
          <HomeSkeleton v-else></HomeSkeleton>
        </transition>
      </div>
    </HomePanel>
  </div>
</template>

<script>
/*
 1.先写好骨架屏结构
 2. 做好v-if与v-else处理
 3.加入动画处理
*/
/*
懒加载:
      1.相应的dom
      2.加载过一次后,再次进入可视区域不在加载了
*/

import { ref } from 'vue'
import HomePanel from './home-panel'
import { homeHot } from '@/api/home'
import HomeSkeleton from './home-skeleton.vue'
import { lazyLoad } from '@/utils/index'
export default {
  name: 'HomeNew',
  components: { HomePanel, HomeSkeleton },
  setup () {
    // const list = ref([])
    // homeHot().then(res => {
    //   list.value = res.result
    // })

    //原生懒加载
    // const target2 = ref(null)
    // function HmIntersectionObserver (target, callback, option) {
    //   const ob = new IntersectionObserver(callback, option)
    //   onMounted(() => {
    //     ob.observe(target.value)
    //   })
    //   function stop () {
    //     ob.unobserve(target.value)
    //   }
    //   return { stop }
    // }
    // const { stop } = HmIntersectionObserver(target2, ([{ isIntersecting }]) => {
    //   if (isIntersecting) {
    //     console.log('显示')
    //   } else {
    //     console.log('隐藏')
    //   }
    // })
    // function btnClick () {
    //   stop()
    // }

    const { list, target } = lazyLoad(homeHot)
    // return { target2, btnClick }
    return { list, target }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
.box {
  position: relative;
  height: 406px;
}
</style>
